package appapi.service.impl;

import appapi.Request.DeviceBlackBoxPageReq;
import appapi.Request.bridge.BlackBoxListReq;
import appapi.Vo.brider.AddIotDevice;
import appapi.Vo.brider.BlackBoxVO;
import appapi.Vo.brider.DeviceVO;
import appapi.Vo.brider.EditDevice;
import appapi.common.ResponseBean;
import appapi.dao.CompanyMapper;
import appapi.dao.ProjectMapper;
import appapi.entity.basic.UserInfo;
import appapi.entity.record.RealDataEntity;
import appapi.mapper.DeviceBlackBoxMapper;
import appapi.entity.Zhongjiao.CompanyDto;
import appapi.entity.Zhongjiao.ProjectDto;
import appapi.entity.bridge.IotDevice;
import appapi.service.DeviceBlackBoxService;
import appapi.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * @author hlm
 * @date
 * @description: 黑匣子
 */
@Service
public class DeviceBlackBoxServiceImpl extends ServiceImpl<DeviceBlackBoxMapper, IotDevice> implements DeviceBlackBoxService {
    @Resource
    private DeviceBlackBoxMapper deviceBlackBoxMapper;
    @Resource
    private CompanyMapper companyMapper;
    @Resource
    private ProjectMapper projectMapper;
    /**
     * 分页查询黑匣子列表
     *
     * @return
     */
    @Override
    public IPage<DeviceVO> getBoxListByPage(DeviceBlackBoxPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<DeviceVO> deviceVOList = deviceBlackBoxMapper.selectDeviceList(req);
        PageInfo<DeviceVO> info = new PageInfo<>(deviceVOList);
        IPage<DeviceVO> deviceVOIPage = new Page<>();
        if (deviceVOList.size() > 0) {
            deviceVOIPage.setRecords(deviceVOList);
            deviceVOIPage.setSize(req.getPageSize());
            deviceVOIPage.setTotal(info.getTotal());
            deviceVOIPage.setCurrent(req.getPageNum());
        }
        return deviceVOIPage;
    }

    /**
     * 查询黑匣子列表
     *
     * @return
     */
    @Override
    public List<DeviceVO> getBoxList(DeviceBlackBoxPageReq req) {
        List<DeviceVO> deviceVOList = deviceBlackBoxMapper.selectDeviceList(req);
        return deviceVOList;
    }

    /**
     * 启用
     *
     * @return
     */
    @Override
    public void insertDevice(AddIotDevice addIotDevice, UserInfo userInfo) {
        IotDevice iotDevice = BeanCopyUtils.copyBean(addIotDevice, IotDevice.class);
        if(iotDevice.getDeviceIp()==null)
        {
            iotDevice.setDeviceIp("");
        }
        if(iotDevice.getDeviceStatus()==null)
        {
            iotDevice.setDeviceStatus(0);
        }
        iotDevice.setCreateTime(LocalDateTime.now());
        iotDevice.setCreateUser(userInfo.getUserid());
        iotDevice.setLastModifyUser(userInfo.getUserid());
        iotDevice.setLastModifyTime(LocalDateTime.now());
        //0:未删除 1:删除
        iotDevice.setDeleteFlag(0);
        //在线状态
        iotDevice.setDeviceOnline(1);
        deviceBlackBoxMapper.insert(iotDevice);
    }

    /**
     * 编辑黑匣子
     *
     * @return
     */
    @Override
    public void editDevice(EditDevice editDevice, UserInfo userInfo) {
        IotDevice iotDevice = BeanCopyUtils.copyBean(editDevice, IotDevice.class);
        iotDevice.setLastModifyTime(LocalDateTime.now());
        iotDevice.setLastModifyUser(userInfo.getUserid());
        deviceBlackBoxMapper.updateById(iotDevice);
    }

    /**
     * 删除设备
     *
     * @return
     */
    @Override
    public void delDevice(Long deviceId) {
        IotDevice iotDevice = deviceBlackBoxMapper.selectById(deviceId);
        if (iotDevice != null) {
            iotDevice.setDeleteFlag(1);
            deviceBlackBoxMapper.updateById(iotDevice);
        }
    }

    /**
     * 停用
     *
     * @return
     */
    @Override
    public ResponseBean stopDevice(Long id, UserInfo userInfo) {
        IotDevice iotDevice = deviceBlackBoxMapper.selectById(id);
        if (iotDevice == null) {
            return ResponseBean.fail("该黑匣子不存在,请输入正确的黑匣子Id");
        }
        iotDevice.setDeviceStatus(1);
        iotDevice.setLastModifyUser(userInfo.getUserid());
        iotDevice.setLastModifyTime(LocalDateTime.now());
        deviceBlackBoxMapper.updateById(iotDevice);
        return ResponseBean.success();
    }

    /**
     * 详情
     *
     * @return
     */
    @Override
    public ResponseBean<DeviceVO> getDeviceModel(Long deviceId) {
        IotDevice iotDevice = deviceBlackBoxMapper.selectById(deviceId);
        DeviceVO deviceVO = BeanCopyUtils.copyBean(iotDevice, DeviceVO.class);
        //封装项目信息
        ProjectDto projectDto = projectMapper.selectByPrimaryKey(Long.valueOf(deviceVO.getProjId()));
        if (projectDto != null) {
            deviceVO.setProjName(projectDto.getProjName());
            //封装公司信息
            List<CompanyDto> companyDtoList = companyMapper.selectByCompanyByOrgaId(projectDto.getOrgaId());
            if (companyDtoList != null && companyDtoList.size() > 0) {
                deviceVO.setCompName(companyDtoList.get(0).getCompName());
            }
        }
        return ResponseBean.success(deviceVO);
    }

    /**
     * 更新终端在线状态
     *
     * @param deviceSn
     * @param deviceStatus
     * @return
     */
    @Override
    public Integer updateDeviceStatus(String deviceSn, String deviceStatus,Long timestamp)
    {
       LocalDateTime updateTime= LocalDateTime.ofInstant(Instant.ofEpochMilli(timestamp), ZoneId.systemDefault());
       //[{"object":"CMCFS","status":"online"}]
       if("online".equals(deviceStatus))
       {
           deviceStatus="0";
       }else{
           deviceStatus="1";
       }
       return deviceBlackBoxMapper.updateDeviceStatus(deviceSn,deviceStatus,updateTime);
    }

    /**
     * 查询未绑定黑匣子列表
     *
     * @return
     */
    @Override
    public List<BlackBoxVO> getBlackBoxList(BlackBoxListReq req) {
        List<BlackBoxVO> blackBoxVOList = deviceBlackBoxMapper.getBlackBoxList(req);
        return blackBoxVOList;
    }

    @Override
    public Integer addRealData(RealDataEntity entity)
    {
      return   deviceBlackBoxMapper.insertRealData(entity);
    }
}
